package administrador;
import java.util.Calendar;
import net.sf.jasperreports.engine.JRException;
public class report_DtlInvent extends javax.swing.JFrame {
    calendario.JCalendar jCalendar = null;
    /** Creates new form report_DtlInvent */
    Conexion conexion;
    javax.swing.JTable tabla;
    javax.swing.JScrollPane js;
    public report_DtlInvent(Conexion conexion) {
        initComponents();
        jCalendar = new calendario.JCalendar(this, "Calendario", true);
        this.conexion=conexion;
        setVisible(true);
        java.sql.ResultSet respuesta=conexion.consultarBase("Select * from Inventario");
        muestroTabla(respuesta);
    }
    public void imprimirHistorialInv(String nombreArchivo){
        
        if(txt_CodInventario.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Codigo de Inventario","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
                String path = "./Reportes/";
                java.util.Map parametros = new java.util.HashMap();
                parametros.put("cod_Inventario",Integer.parseInt(txt_CodInventario.getText()));
                try {
                        generaReporte(path+ nombreArchivo,parametros);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }  
        }
    }
    public void imprimirHistorialInv2(String nombreArchivo){
        if(txt_FechaIni.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha de Inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
            if(txt_fchFin.getText().equals("")){
                javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha Fin","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
            }else{
                     if(txt_FechaIni.getText().compareTo(txt_fchFin.getText())<=0){
                            String path = "./Reportes/";
                            java.util.Map parametros = new java.util.HashMap();
                            parametros.put("fecha_Inicio",fecha1);
                            parametros.put("fecha_Fin",fecha2);
                            try {
                                    generaReporte(path+ nombreArchivo,parametros);
                            } catch (Exception e) {
                                    System.out.println(e.getMessage());
                            }  
                    }else{
                        javax.swing.JOptionPane.showMessageDialog(this,"La fecha fin no puede ser menor que la fecha de inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
                    }
            }
        }
    }
    
    public void  generaReporte(String path,java.util.Map parametros) throws JRException{
        net.sf.jasperreports.engine.JasperReport report = net.sf.jasperreports.engine.JasperCompileManager.compileReport(path);
        net.sf.jasperreports.engine.JasperPrint print = net.sf.jasperreports.engine.JasperFillManager.fillReport(report, parametros, conexion.getconexion());
        net.sf.jasperreports.view.JasperViewer.viewReport(print,false);
    }
    public void muestroTabla(java.sql.ResultSet respuesta){
     //Obtengo Filas-Arreglo de Titulos de Columnas
        int filas=conexion.obtenerFilas(respuesta);
        String[] titulosCol=conexion.obtenerColumnas(respuesta);
        //Creo la tabla:
        CrearTabla(filas,titulosCol);
        llenaTabla(respuesta,titulosCol.length,filas);
    }
    public void CrearTabla(int filas,String[] titulos){
        
        int columnas=titulos.length;
        String[][] tabla0=new String[filas][columnas];
       
        tabla= new javax.swing.JTable(tabla0,titulos);
        
        //Aqui le especifico para que la tabla acepte valores doubles e int:
        //--------------------------------------------------------------------
        final boolean[] canEdit = new boolean [columnas];
        for(int p=0;p<columnas;p++){
        canEdit[p]=false;
        }
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            tabla0,titulos  
        ) {
           // boolean[] canEdit = new boolean [] { false, false, false, false};
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        //---------------------------------------------------------------------
                   
        //Le doy el evento del LCick del Mouse:
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        
        //remuevo el JScroll para que no se monte cuando cree el nuevo:
        //*************************************************************
        try{
        panel_tabla.remove(js);
        }catch(java.lang.NullPointerException e){}
        //*************************************************************
        //Pongo en la tabla:
            js = new javax.swing.JScrollPane(tabla);
            js.setBounds(0,0,669,395);
            js.setVisible(true);
            panel_tabla.add(js);
            js.updateUI();
    }
    java.sql.Date fecha1=null;
    java.sql.Date fecha2=null;
    public void sacarFecha(int opcion) {
        Calendar cDate = jCalendar.getCalendar();
        if (cDate == null) {
            cDate = Calendar.getInstance();
        }
        jCalendar.setCalendar(cDate);
        jCalendar.setVisible(true);
        if (jCalendar.isOkPressed()) {
            if (opcion == 1) {
                fecha1 = new java.sql.Date(jCalendar.getDate().getTime());
                System.out.println(fecha1);
                txt_FechaIni.setText(fecha1.toString());
            } else if (opcion == 2) {
                fecha2 = new java.sql.Date(jCalendar.getDate().getTime());
                txt_fchFin.setText(fecha2.toString());
            }
            jCalendar.setVisible(false);
        }
    }
    public void llenaTabla(java.sql.ResultSet r,int columnas,int filas){
        try{
            r.beforeFirst();
            
            int j=0;
            while(r.next()) {
                
                for(int i=0;i<columnas;i++) {
                    
                    Object toma=r.getObject(i+1);
                    //System.out.println("toma valor:"+toma+" * fila:"+j+" * column:"+i);
                    try{
                        tabla.setValueAt(toma,j,i);
                    }catch(java.lang.ArrayStoreException e){
                        System.out.print("Errrrorrr");
                        tabla.setValueAt("null",j,i);
                    }
                }
                
                j++;
            }
        }catch(java.sql.SQLException sqle){
            System.out.println("Error al mostrar los datos");
            System.err.println(sqle);
        }
    }
    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {
        txt_CodInventario.setText(tabla.getValueAt(tabla.getSelectedRow(),0).toString());
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel_tabla = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        txt_CodInventario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txt_FechaIni = new javax.swing.JTextField();
        txt_fchFin = new javax.swing.JTextField();
        btn_FechaInicio = new javax.swing.JButton();
        btn_FechaFin = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        panel_tabla.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        panel_tabla.setName("panel_tabla"); // NOI18N

        javax.swing.GroupLayout panel_tablaLayout = new javax.swing.GroupLayout(panel_tabla);
        panel_tabla.setLayout(panel_tablaLayout);
        panel_tablaLayout.setHorizontalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 659, Short.MAX_VALUE)
        );
        panel_tablaLayout.setVerticalGroup(
            panel_tablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 386, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setName("jPanel3"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Detalle de Inventario"));
        jPanel1.setName("jPanel1"); // NOI18N

        txt_CodInventario.setName("txt_CodInventario"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(report_DtlInvent.class);
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(txt_CodInventario, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(23, 23, 23))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addContainerGap(52, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(txt_CodInventario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Historial de Inventarios"));
        jPanel2.setName("jPanel2"); // NOI18N

        txt_FechaIni.setEditable(false);
        txt_FechaIni.setName("txt_FechaIni"); // NOI18N

        txt_fchFin.setEditable(false);
        txt_fchFin.setName("txt_fchFin"); // NOI18N

        btn_FechaInicio.setIcon(resourceMap.getIcon("btn_FechaInicio.icon")); // NOI18N
        btn_FechaInicio.setName("btn_FechaInicio"); // NOI18N
        btn_FechaInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_FechaInicioActionPerformed(evt);
            }
        });

        btn_FechaFin.setIcon(resourceMap.getIcon("btn_FechaFin.icon")); // NOI18N
        btn_FechaFin.setName("btn_FechaFin"); // NOI18N
        btn_FechaFin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_FechaFinActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txt_fchFin)
                    .addComponent(txt_FechaIni, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_FechaFin, 0, 0, Short.MAX_VALUE)
                    .addComponent(btn_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE))
                .addGap(16, 16, 16)
                .addComponent(jButton3)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_FechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_fchFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_FechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(panel_tabla, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_tabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-707)/2, (screenSize.height-555)/2, 707, 555);
    }// </editor-fold>//GEN-END:initComponents

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        imprimirHistorialInv("Inventario/"+"DetalleInventario.jrxml");
}//GEN-LAST:event_jButton2ActionPerformed

private void btn_FechaInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_FechaInicioActionPerformed
sacarFecha(1);
}//GEN-LAST:event_btn_FechaInicioActionPerformed

private void btn_FechaFinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_FechaFinActionPerformed
sacarFecha(2);
}//GEN-LAST:event_btn_FechaFinActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
imprimirHistorialInv2("Inventario/"+"contenedor.jrxml");
}//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_FechaFin;
    private javax.swing.JButton btn_FechaInicio;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel panel_tabla;
    public javax.swing.JTextField txt_CodInventario;
    private javax.swing.JTextField txt_FechaIni;
    private javax.swing.JTextField txt_fchFin;
    // End of variables declaration//GEN-END:variables

}
